const mongoose = require('mongoose');
const Food = require('./models/Food');

// 连接数据库
const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/traveller_social_app', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log('📦 MongoDB已连接');
  } catch (error) {
    console.error('❌ 数据库连接失败:', error);
    process.exit(1);
  }
};

// 美食初始化数据
const foodsData = [
  {
    name: "麻辣火锅",
    description: "正宗四川麻辣火锅，精选优质食材，麻辣鲜香，回味无穷。配有丰富的蘸料和小菜，是聚餐的首选。",
    category: "火锅",
    price: 88,
    originalPrice: 108,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1563379091339-03246963d50a?w=800",
      "https://images.unsplash.com/photo-1582878826629-29b7ad1cdc43?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1563379091339-03246963d50a?w=800",
    restaurant: {
      name: "蜀香火锅",
      address: "四川省成都市锦江区春熙路108号",
      phone: "028-8888-6666",
      coordinates: {
        latitude: 30.6598,
        longitude: 104.0633
      },
      businessHours: "11:00-23:00"
    },
    rating: 4.8,
    reviewCount: 1567,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "美食达人小王",
        userAvatar: "https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=100",
        rating: 5,
        content: "非常正宗的四川火锅！麻辣味道刚刚好，食材新鲜，服务态度也很好。推荐毛肚和鸭血，特别嫩！",
        images: ["https://images.unsplash.com/photo-1563379091339-03246963d50a?w=400"],
        createdAt: new Date("2024-12-01"),
        helpful: 23
      },
      {
        userId: "6872826501b094a9bfe8e056",
        userName: "火锅爱好者",
        userAvatar: null,
        rating: 4,
        content: "环境不错，火锅味道也很棒，就是人有点多需要排队。但等待是值得的！",
        images: [],
        createdAt: new Date("2024-12-15"),
        helpful: 12
      }
    ],
    tags: ["麻辣", "正宗", "聚餐", "网红"],
    features: ["招牌菜", "网红推荐"],
    ingredients: ["牛油", "辣椒", "花椒", "各种蔬菜", "肉类"],
    taste: {
      spicy: 5,
      sweet: 1,
      sour: 0,
      salty: 3
    },
    salesCount: 2341,
    monthlyOrders: 156,
    isAvailable: true,
    isRecommended: true,
    isHot: true,
    discount: "首单立减20元"
  },
  {
    name: "白切鸡",
    description: "广东传统名菜，选用优质土鸡，肉质鲜嫩，配以秘制姜蓉酱，清香不腻，营养丰富。",
    category: "粤菜",
    price: 58,
    originalPrice: 68,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1565299624946-b28f40a0ca4b?w=800",
      "https://images.unsplash.com/photo-1594221708779-94832f4320d1?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1565299624946-b28f40a0ca4b?w=800",
    restaurant: {
      name: "粤味轩",
      address: "广东省广州市天河区体育西路88号",
      phone: "020-8888-9999",
      coordinates: {
        latitude: 23.1291,
        longitude: 113.3417
      },
      businessHours: "10:30-22:30"
    },
    rating: 4.6,
    reviewCount: 892,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "粤菜专家",
        userAvatar: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=100",
        rating: 5,
        content: "正宗的白切鸡！鸡肉嫩滑，姜蓉酱调得刚好，一点都不腥。这家店的功夫真的到家了。",
        images: [],
        createdAt: new Date("2024-11-20"),
        helpful: 18
      }
    ],
    tags: ["清淡", "营养", "传统"],
    features: ["老字号", "招牌菜"],
    nutrition: {
      calories: 165,
      protein: 31,
      carbs: 0,
      fat: 3.6,
      fiber: 0
    },
    ingredients: ["土鸡", "生姜", "葱", "料酒"],
    taste: {
      spicy: 0,
      sweet: 1,
      sour: 0,
      salty: 2
    },
    salesCount: 1567,
    monthlyOrders: 89,
    isAvailable: true,
    isRecommended: true,
    isHot: false
  },
  {
    name: "小笼包",
    description: "上海经典小吃，皮薄馅大，汤汁丰富，一口一个，鲜美无比。现包现蒸，保证新鲜。",
    category: "小吃",
    price: 25,
    originalPrice: 30,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1496116218417-1a781b1c416c?w=800",
      "https://images.unsplash.com/photo-1563245372-f21724e3856d?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1496116218417-1a781b1c416c?w=800",
    restaurant: {
      name: "老上海小笼包",
      address: "上海市黄浦区南京东路168号",
      phone: "021-6666-8888",
      coordinates: {
        latitude: 31.2304,
        longitude: 121.4737
      },
      businessHours: "07:00-21:00"
    },
    rating: 4.7,
    reviewCount: 2134,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "小笼包控",
        userAvatar: null,
        rating: 5,
        content: "每次来上海必吃！皮真的很薄，汤汁很多，咬开的时候要小心烫嘴。味道一如既往的好！",
        images: ["https://images.unsplash.com/photo-1496116218417-1a781b1c416c?w=400"],
        createdAt: new Date("2024-12-10"),
        helpful: 31
      }
    ],
    tags: ["传统", "经典", "早餐", "汤包"],
    features: ["老字号", "网红推荐"],
    ingredients: ["面粉", "猪肉", "高汤", "韭黄"],
    taste: {
      spicy: 0,
      sweet: 2,
      sour: 0,
      salty: 3
    },
    salesCount: 3421,
    monthlyOrders: 267,
    isAvailable: true,
    isRecommended: true,
    isHot: true,
    discount: "买2笼送1笼"
  },
  {
    name: "红烧肉",
    description: "江南传统名菜，选用五花肉慢炖而成，肥瘦相间，入口即化，甜而不腻，是下饭的绝配。",
    category: "苏菜",
    price: 45,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1565680018434-b513d5924171?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1565680018434-b513d5924171?w=800",
    restaurant: {
      name: "江南人家",
      address: "江苏省苏州市姑苏区观前街66号",
      phone: "0512-6789-0123",
      coordinates: {
        latitude: 31.2989,
        longitude: 120.6189
      },
      businessHours: "11:00-20:30"
    },
    rating: 4.5,
    reviewCount: 678,
    reviews: [],
    tags: ["下饭", "传统", "甜味"],
    features: ["招牌菜"],
    ingredients: ["五花肉", "冰糖", "生抽", "老抽", "料酒"],
    taste: {
      spicy: 0,
      sweet: 4,
      sour: 0,
      salty: 3
    },
    salesCount: 987,
    monthlyOrders: 54,
    isAvailable: true,
    isRecommended: false,
    isHot: false
  },
  {
    name: "日式拉面",
    description: "正宗日式豚骨拉面，汤底浓郁，面条劲道，配有叉烧、溏心蛋和海苔，口感层次丰富。",
    category: "日料",
    price: 65,
    originalPrice: 75,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1569718212165-3a8278d5f624?w=800",
      "https://images.unsplash.com/photo-1617093727343-374698b1b08d?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1569718212165-3a8278d5f624?w=800",
    restaurant: {
      name: "一兰拉面",
      address: "北京市朝阳区三里屯太古里南区N4-101",
      phone: "010-8888-1234",
      coordinates: {
        latitude: 39.9042,
        longitude: 116.4074
      },
      businessHours: "11:00-23:30"
    },
    rating: 4.4,
    reviewCount: 1245,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "拉面爱好者",
        userAvatar: "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=100",
        rating: 4,
        content: "汤底很浓郁，面条也很有嚼劲。叉烧肉质不错，溏心蛋也很棒。就是价格稍微贵了点。",
        images: [],
        createdAt: new Date("2024-11-28"),
        helpful: 15
      }
    ],
    tags: ["日式", "汤面", "叉烧"],
    features: ["新品上市"],
    ingredients: ["拉面", "豚骨汤", "叉烧", "溏心蛋", "海苔", "葱花"],
    taste: {
      spicy: 1,
      sweet: 1,
      sour: 0,
      salty: 4
    },
    salesCount: 1876,
    monthlyOrders: 123,
    isAvailable: true,
    isRecommended: false,
    isHot: true
  },
  {
    name: "芝士蛋糕",
    description: "免烤芝士蛋糕，口感顺滑，甜度适中，淡淡的芝士香味配上酸甜的蓝莓酱，是完美的餐后甜点。",
    category: "甜品",
    price: 35,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1565958011703-44f9829ba187?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1565958011703-44f9829ba187?w=800",
    restaurant: {
      name: "甜蜜时光",
      address: "上海市静安区南京西路1038号",
      phone: "021-5555-7777",
      coordinates: {
        latitude: 31.2252,
        longitude: 121.4581
      },
      businessHours: "09:00-22:00"
    },
    rating: 4.3,
    reviewCount: 456,
    reviews: [],
    tags: ["甜品", "芝士", "蓝莓"],
    features: ["健康轻食", "情侣约会"],
    nutrition: {
      calories: 285,
      protein: 6,
      carbs: 28,
      fat: 18,
      fiber: 1
    },
    ingredients: ["奶油奶酪", "淡奶油", "鸡蛋", "糖", "蓝莓酱"],
    taste: {
      spicy: 0,
      sweet: 5,
      sour: 2,
      salty: 1
    },
    salesCount: 723,
    monthlyOrders: 67,
    isAvailable: true,
    isRecommended: false,
    isHot: false
  },
  {
    name: "韩式烤肉",
    description: "精选优质牛肉，韩式腌制，炭火烤制，配以新鲜蔬菜和特制蘸料，肉质鲜美，口感丰富。",
    category: "韩料",
    price: 128,
    originalPrice: 148,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1504674900247-0877df9cc836?w=800",
      "https://images.unsplash.com/photo-1586190848861-99aa4a171e90?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1504674900247-0877df9cc836?w=800",
    restaurant: {
      name: "首尔烤肉",
      address: "北京市海淀区中关村大街32号",
      phone: "010-6666-5555",
      coordinates: {
        latitude: 39.9886,
        longitude: 116.3142
      },
      businessHours: "17:00-24:00"
    },
    rating: 4.6,
    reviewCount: 834,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "烤肉专家",
        userAvatar: null,
        rating: 5,
        content: "肉质很新鲜，腌制得很入味。服务员会帮忙烤，省心省力。泡菜和小菜也很棒！",
        images: ["https://images.unsplash.com/photo-1504674900247-0877df9cc836?w=400"],
        createdAt: new Date("2024-12-05"),
        helpful: 26
      }
    ],
    tags: ["烤肉", "韩式", "聚餐"],
    features: ["网红推荐", "商务宴请"],
    ingredients: ["牛肉", "猪肉", "韩式酱料", "生菜", "泡菜"],
    taste: {
      spicy: 3,
      sweet: 2,
      sour: 1,
      salty: 3
    },
    salesCount: 1654,
    monthlyOrders: 98,
    isAvailable: true,
    isRecommended: true,
    isHot: false,
    discount: "双人套餐9折"
  },
  {
    name: "珍珠奶茶",
    description: "台式珍珠奶茶，香醇奶茶配上Q弹珍珠，甜度可调，冰度可选，是休闲时光的完美伴侣。",
    category: "饮品",
    price: 18,
    currency: "¥",
    images: [
      "https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=800"
    ],
    mainImage: "https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=800",
    restaurant: {
      name: "茶颜悦色",
      address: "湖南省长沙市岳麓区步行街123号",
      phone: "0731-8888-9999",
      coordinates: {
        latitude: 28.2282,
        longitude: 112.9388
      },
      businessHours: "08:00-22:00"
    },
    rating: 4.2,
    reviewCount: 1876,
    reviews: [
      {
        userId: "6872826501b094a9bfe8e055",
        userName: "奶茶控",
        userAvatar: "https://images.unsplash.com/photo-1494790108755-2616b332c3a9?w=100",
        rating: 4,
        content: "珍珠很Q弹，奶茶香甜，价格也很实惠。就是排队的人太多了，需要等一会儿。",
        images: [],
        createdAt: new Date("2024-12-12"),
        helpful: 8
      }
    ],
    tags: ["奶茶", "珍珠", "台式"],
    features: ["网红推荐"],
    ingredients: ["红茶", "牛奶", "珍珠", "糖"],
    taste: {
      spicy: 0,
      sweet: 4,
      sour: 0,
      salty: 0
    },
    salesCount: 4567,
    monthlyOrders: 432,
    isAvailable: true,
    isRecommended: false,
    isHot: true
  }
];

// 初始化美食数据
const initFoodData = async () => {
  try {
    console.log('🍔 开始初始化美食数据...');
    
    // 清除现有数据
    await Food.deleteMany({});
    console.log('🗑️ 已清除现有美食数据');
    
    // 插入新数据
    const foods = await Food.insertMany(foodsData);
    console.log(`✅ 成功插入 ${foods.length} 条美食数据`);
    
    // 显示统计信息
    const stats = await Food.aggregate([
      {
        $group: {
          _id: '$category',
          count: { $sum: 1 },
          avgRating: { $avg: '$rating' },
          avgPrice: { $avg: '$price' }
        }
      },
      { $sort: { count: -1 } }
    ]);
    
    console.log('\n📊 美食分类统计:');
    stats.forEach(stat => {
      console.log(`  ${stat._id}: ${stat.count}个, 平均评分: ${stat.avgRating.toFixed(1)}, 平均价格: ¥${stat.avgPrice.toFixed(0)}`);
    });
    
    console.log('\n🎉 美食数据初始化完成！');
    
  } catch (error) {
    console.error('❌ 美食数据初始化失败:', error);
  }
};

// 运行初始化
const main = async () => {
  await connectDB();
  await initFoodData();
  await mongoose.connection.close();
  console.log('📦 数据库连接已关闭');
  process.exit(0);
};

main().catch(error => {
  console.error('❌ 程序执行失败:', error);
  process.exit(1);
}); 